<?php

namespace Cframe\Methods;


use Cframe\Log;

abstract class ModelMethod{
    private $condition = '';
    private $alise = '';

    public function key_con($str)
    {
        if (empty($str)) {
            return $str;
        }
        switch (strtolower($str)) {
            case 'eq':
                $str = "=";
                break;
            case 'neq':
                $str = '!=';
                break;
            case 'lte':
                $str = '<=';
                break;
            case 'lt':
                $str = '<';
                break;
            case 'gte':
                $str = '>=';
                break;
            case 'gt':
                $str = '>';
                break;
        }
        return $str;
    }

    public function con_string($str, $auto = false)
    {
        $sign = [',', '|'];
        $string = $str;
        foreach ($sign as $k) {
            if (strpos($str, $k)) {
                $str_a = explode($k, $str);
                array_walk($str_a, function (&$value) use ($auto) {
                    if (!is_numeric($value) && !$auto) {
                        $value = "'{$value}'";
                    }
                });
                $str = implode($k, $str_a);
                break;
            }
        }
        if (strcmp($str, $string) == 0) {
            return "'{$str}'";
        }
        return $str;
    }

    public function con_field($str, $auto = false)
    {
        $sign = [',', '|'];
        $string = $str;
        $alise = !empty($this->alise) ? $this->alise . '.' : '';
        foreach ($sign as $k) {
            if (strpos($str, $k)) {
                $str_a = explode($k, $str);
                array_walk($str_a, function (&$value) use ($auto, $alise) {
                    if (!is_numeric($value) && !$auto) {
                        $value = "{$alise}`{$value}`";
                    }
                });
                $str = implode($k, $str_a);
                break;
            }
        }
        if (strcmp($str, $string) == 0) {
            return "{$alise}`{$str}`";
        }
        return $str;
    }
}